package com.logic.modular.gameCore.config.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.comment.util.message.ResponseResult;
import com.logic.modular.gameCore.config.service.impl.CoreConfigServiceImpl;
import com.comment.pojo.domain.logic.core.CoreConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

/**
 * 战斗配置(CoreConfig)表服务控制层
 *
 * @author cxy
 * @since 2024-11-18 12:06:09
 */
@Api(tags = "战斗配置(CoreConfig)")
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("/battleConfig")
public class BattleConfigController {
    @Resource
    private CoreConfigServiceImpl battleConfigService;

    @ApiOperation("获取战斗配置列表")
    @PostMapping("/list")
    public ResponseResult<List<CoreConfig>> list() {
        return ResponseResult.success(battleConfigService.listCache());
    }

    @SneakyThrows(IOException.class)
    @ApiOperation(value = "下载配置表",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @RequestMapping(value = "/downloadInfo", method = RequestMethod.GET)
    public void download(HttpServletResponse response) {
        //设置响应字符集
        response.setCharacterEncoding("UTF-8");
        //设置响应媒体类型
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename="+"战斗配置表.xlsx");

        List<CoreConfig> memberList = battleConfigService.listCache();
        EasyExcel.write(response.getOutputStream(),CoreConfig.class)
                .head(CoreConfig.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("core_config")
                .doWrite(memberList);
    }

}
